package com.yycx.module.bus.provider.handler.haiying.tasktodo.refundamount;

import com.yycx.bpm.provider.listener.FlowPageInterceptor;
import com.yycx.bpm.provider.model.TaskResponse;
import com.yycx.common.base.entity.EntityMap;
import com.yycx.common.base.utils.FlymeUtils;
import com.yycx.common.security.OpenHelper;
import com.yycx.common.utils.DateUtils;
import com.yycx.module.admin.provider.service.BaseUserService;
import com.yycx.module.bus.client.entity.BusPayment;
import com.yycx.module.bus.provider.service.BusPaymentService;
import org.flowable.common.engine.api.query.Query;
import org.flowable.task.api.TaskInfoQuery;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 退款资金支付审批任务列表
 *
 * @author zyf
 */
@Component
public class RefundAmountTab1TodoListHandler implements FlowPageInterceptor<TaskResponse> {

    @Resource
    private BusPaymentService paymentService;

    @Resource
    private BaseUserService userService;

    @Override
    public void prepare(Query query, EntityMap params) {
        TaskInfoQuery taskQuery = (TaskInfoQuery) query;
        Long companyId = OpenHelper.getCompanyId();
        String paymentNo = params.get("paymentNo");
        BusPayment payment = paymentService.getByProperty("paymentNo", paymentNo);
        if (FlymeUtils.isNotEmpty(payment)) {
            taskQuery.processInstanceBusinessKey(payment.getPaymentId().toString());
        } else {
            taskQuery.processInstanceBusinessKey(paymentNo);
        }
        taskQuery.processVariableValueEquals("companyId", companyId);
        //排除发起人节点
        taskQuery.taskDefinitionKeyLike("Activity_%");
        taskQuery.taskTenantId("flp");
        taskQuery.processDefinitionKey("process_refund_hk_pay_flp");
    }

    @Override
    public List<EntityMap> complete(Query query, List<TaskResponse> data, EntityMap params) {
        List<EntityMap> list = new ArrayList<>();
        if (FlymeUtils.isNotEmpty(data)) {
            for (TaskResponse taskResponse : data) {
                EntityMap map = new EntityMap();
                String paymentId = taskResponse.getBusinessKey();
                String processInstanceId = taskResponse.getProcessInstanceId();
                String assignee = taskResponse.getAssignee();
                if (FlymeUtils.isNotEmpty(paymentId)) {
                    BusPayment payment = paymentService.getById(paymentId);
                    if (FlymeUtils.isNotEmpty(payment)) {
                        //单据类型
                        String amountType = payment.getAmountType();
                        map.put("nickName", payment.getNickName());
                        //收款方
                        map.put("payeeName", payment.getPayeeName());
                        map.put("paymentNo", payment.getPaymentNo());
                        map.put("paymentId", payment.getPaymentId());
                        map.put("bankName", payment.getBankName());
                        map.put("bankNo", payment.getBankNo());
                        map.put("amountType", amountType);
                        map.put("drugType", payment.getDrugType());
                        map.put("amount", payment.getAmount());
                        map.put("createTime", payment.getCreateTime());
                        map.put("amountDate", DateUtils.formatDate(payment.getAmountDate(), "yyyy-MM-dd"));
                        map.put("paymentState", payment.getPaymentState());
                    }
                    map.put("assignee", assignee);
                    map.put("taskName", taskResponse.getName());
                    map.put("taskId", taskResponse.getId());
                    map.put("endTime", taskResponse.getEndTime());
                    map.put("processInstanceId", processInstanceId);
                    list.add(map);
                }
            }
        }
        return list;
    }


}
